//
//  HRZClassifyGoodsListViewController.m
//  EMarket
//
//  Created by 石头 on 2018/8/29.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZClassifyGoodsListViewController.h"
// Controllers
#import "HRZGoodsDetailViewController.h"
// Models
#import "HRZGoodsClassifyContentModel.h"
#import "HRZGoodsClassifyDetailModel.h"
#import "HRZGoodsClassifyListSearchModel.h"
#import "HRZ_ClassMainItem.h"
// Views
#import "HRZ_HoverFlowLayout.h"
#import "HRZ_BrandsSortHeaderView.h"
#import "HRZShowGoodsSimpleInfoCell.h"

// Vendors

// Categories

// Others
#import "HRZGlobalNetManager.h"


@interface HRZClassifyGoodsListViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    HRZGoodsClassifyListSearchModel *_searchModel;
}

@property (nonatomic, strong) NSMutableArray <HRZGoodsClassifyDetailModel *>*goodsItems;

@end


static NSString *const HRZ_BradndsSortHeaderID = @"HRZ_BradndsSortHeaderView";
static NSString *const HRZShowGoodsSimpleInfoCellID = @"HRZShowGoodsSimpleInfoCell";

@implementation HRZClassifyGoodsListViewController
@synthesize collectionView = _collectionView;

#pragma mark - ----------  lazyLoad  ----------
- (NSMutableArray *)goodsItems{
    if (!_goodsItems) {
        _goodsItems = [[NSMutableArray alloc] init];
    }
    return _goodsItems;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        HRZ_HoverFlowLayout *layout = [HRZ_HoverFlowLayout new];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - HRZTopNavH) collectionViewLayout:layout];
        _collectionView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - HRZTopNavH);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        /** 头部view */
        [_collectionView registerNib:[UINib nibWithNibName:@"HRZ_BrandsSortHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HRZ_BradndsSortHeaderID];
        
        /** 商品view */
        [_collectionView registerClass:[HRZShowGoodsSimpleInfoCell class] forCellWithReuseIdentifier:HRZShowGoodsSimpleInfoCellID];
        
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

#pragma mark - ----------  lifeCycle  ----------
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpUI];
    
    [self setUpData];
}

#pragma mark - ----------  Setter  ----------
- (void)setType:(GoodsListSearchType)type {
    _type = type;
}

- (void)setSearchKey:(NSString *)searchKey {
    _searchKey = searchKey;
}

- (void)setTitleName:(NSString *)titleName {
    _titleName = titleName;
}

#pragma mark - ----------  Action  ----------
- (void)setUpUI {
    
    self.view.backgroundColor = HRZBGColor;
    self.collectionView.backgroundColor = self.view.backgroundColor;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    
    self.navigationController.navigationBar.barTintColor = HRZBGColor;
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text = _titleName;
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor blackColor];
    self.navigationItem.titleView = title;
}

#pragma mark - 加载数据
- (void)setUpData {
    _searchModel = [HRZGoodsClassifyListSearchModel new];
    _searchModel.page = 1;
    _searchModel.limit = 20;
    switch (_type) {
        case GoodsListSearchTypeSearch:
        {
            _searchModel.search = _searchKey;
        }
            break;
        case GoodsListSearchTypeClassify:
        {
            _searchModel.classifyId = [@"1" integerValue];
//            _searchModel.classifyId = [_searchKey integerValue];
        }
            break;
        case GoodsListSearchTypeBrand:
        {
            _searchModel.trademarkId = _searchKey;
        }
            break;
    }
    
    [self beginSearchData];
}

#pragma mark - ----------  method  ----------
- (void)beginSearchData{
    WEAKSELF
    [[HRZGlobalNetManager getGlobalNetManager] onSearchGoodsListBySerachModel:_searchModel complete:^(NSArray<HRZGoodsClassifyDetailModel *> *result, BOOL isSuc, BOOL isMore) {
        
        if (isSuc) {
            if (isMore) {
                [weakSelf.goodsItems addObjectsFromArray:result];
            } else {
                weakSelf.goodsItems = [NSMutableArray arrayWithArray:result];
            }
        } else {

            NSMutableArray *arr = [NSMutableArray array];
            for (int i = 0; i < arc4random() %9 + 4; i++) {
                HRZGoodsClassifyDetailModel *goods = [HRZGoodsClassifyDetailModel initBaseModel];
                [arr addObject:goods];
            }
            [weakSelf.goodsItems addObjectsFromArray:arr];
        }
        [weakSelf.collectionView reloadData];
    }];
}

- (void)searchMoreData {
    WEAKSELF
    [[HRZGlobalNetManager getGlobalNetManager] onSearchMoreGoodsListComplete:^(NSArray<HRZGoodsClassifyDetailModel *> *result, BOOL isSuc, BOOL isMore) {
        
        if (isSuc) {
            if (isMore) {
                [weakSelf.goodsItems addObjectsFromArray:result];
            } else {
                weakSelf.goodsItems = [NSMutableArray arrayWithArray:result];
            }
        } else {
            
        }
        [weakSelf.collectionView reloadData];
    }];
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _goodsItems.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HRZShowGoodsSimpleInfoCell *cell = nil;
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:HRZShowGoodsSimpleInfoCellID forIndexPath:indexPath];
    cell.model = _goodsItems[indexPath.row];
    
    return cell;
}

#pragma mark - ------<UICollectionViewDelegate>------
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        HRZ_BrandsSortHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HRZ_BradndsSortHeaderID forIndexPath:indexPath];
        HRZ_ClassMainItem *item = [[HRZ_ClassMainItem alloc] init];
        
        headerView.headerTitle = item;
        reusableView = headerView;
        
    }
    return reusableView;
}

#pragma mark - item--Width-Height
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((kScreenWidth - 4)/2, (kScreenWidth - 4)/2 + 60);
}

#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 4;
    
}
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 4;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HRZGoodsDetailViewController *goodsDeatilVC = [[HRZGoodsDetailViewController alloc] init];
    
    HRZGoodsClassifyDetailModel *detailModel = self.goodsItems[indexPath.item];
    goodsDeatilVC.goodsId = detailModel.goodsId;
    goodsDeatilVC.goodPrice = detailModel.price;
    goodsDeatilVC.goodTitle = detailModel.goodsName;
    goodsDeatilVC.goodImageView = detailModel.goodsImg;
    
    [self.navigationController pushViewController:goodsDeatilVC animated:YES];
}

#pragma mark - ----------  Other  ----------

@end
